package com.firesys.rounting.Adapter;

import java.util.List;


import com.firesys.R;
import com.firesys.povo.DeviceList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 分组适配器
 * @author Administrator
 *
 */
public class MyListAdapter extends BaseExpandableListAdapter{
	
	public Activity  activity;
	
	private List<DeviceList>  child;
	
	private OnClickListener  listener;
	
	
	
	public List<DeviceList> getChild() {
		return child;
	}

	public void setChild(List<DeviceList> child) {
		this.child = child;
	}

	public MyListAdapter(Activity activity,List<DeviceList>  list,OnClickListener  listener){
		this.activity=activity;
		this.child=list;
		this.listener=listener;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		
		return child.get(groupPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		
		return 0;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ChildHolder   childholder=null;
		 DeviceList  device=child.get(groupPosition);
		if(convertView==null){
			convertView=LayoutInflater.from(activity).inflate(R.layout.device_list_child, null);
			childholder=new ChildHolder();
			childholder.textview1=(TextView)convertView.findViewById(R.id.device_list_startdate);
			childholder.textview2=(TextView)convertView.findViewById(R.id.device_list_enddate);
			childholder.textview3=(TextView)convertView.findViewById(R.id.device_list_total);
			childholder.textview4=(TextView)convertView.findViewById(R.id.device_list_nocopy);
			childholder.button1=(Button)convertView.findViewById(R.id.device_list_go);
			childholder.button2=(Button)convertView.findViewById(R.id.device_list_query);
			childholder.button3=(Button)convertView.findViewById(R.id.device_list_upload);
			childholder.button1.setOnClickListener(listener);
			childholder.button2.setOnClickListener(listener);
			childholder.button3.setOnClickListener(listener);
			
			convertView.setTag(childholder);
		}else{
			childholder=(ChildHolder)convertView.getTag();
		}
		childholder.textview1.setText(device.getStartDate());
		childholder.textview2.setText(device.getEndDate());
		childholder.textview3.setText(device.getTotal()+"");
		childholder.textview4.setText(device.getNocopy()+"");
		childholder.button1.setTag(groupPosition+"");
		childholder.button2.setTag(groupPosition+"");
		childholder.button3.setTag(groupPosition+"");
		
		
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		
		return 1;
	}

	@Override
	public Object getGroup(int groupPosition) {
		
		return child.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		
		return child.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		DeviceList   groupdevice=child.get(groupPosition);
		GroupView   groupholder=null;
		if(convertView==null){
			convertView=LayoutInflater.from(activity).inflate(R.layout.device_list_group, null);
			groupholder=new GroupView();
			groupholder.image=(ImageView)convertView.findViewById(R.id.tag_img);
			groupholder.textview=(TextView)convertView.findViewById(R.id.title_view);
			convertView.setTag(groupholder);
		}else{
			groupholder=(GroupView)convertView.getTag();
		}
		
		if(!isExpanded){
			groupholder.image.setImageResource(R.drawable.list_indecator_button);
		}else{
			groupholder.image.setImageResource(R.drawable.list_indecator_button_down);
		}
		groupholder.textview.setText(groupdevice.getDeviceListName());
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		
		return false;
	}

	
	//子级
	 class ChildHolder{
		public TextView  textview1;//开始时间
		public TextView  textview2;//结束时间
		public TextView  textview3;//设备册总数
		public TextView  textview4;//设备册未抄数
		public Button  button1;//执行
		public Button  button2;//查询
		public Button  button3;//上传
		
		
	}
	 
	 //组级
	 class GroupView{
			
			private TextView  textview;
			
			private ImageView  image;
				
		}

}
